package com.jintian.smart.kernel.common.context;

import cn.hutool.core.convert.Convert;
import com.jintian.smart.kernel.common.constant.StringPool;
import com.jintian.smart.kernel.common.util.ThreadLocalUtil;
import com.jintian.smart.kernel.core.constant.SmartConstant;
import com.jintian.smart.kernel.core.system.RequestEmployee;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 请求用户  工具类
 */
@Slf4j
public class ContextHolder {
    //region 存储参数
    public static void setUser(RequestEmployee requestUser) {
        if (requestUser == null) {
            return;
        }
        set(SmartConstant.USER, requestUser);
        setUserId(requestUser.getUserId().toString());
        setUserName(requestUser.getUserName());
    }

    public static RequestEmployee getUser() {
        return get(SmartConstant.USER, RequestEmployee.class);
    }

    /**
     * 用户ID
     *
     * @return 用户ID
     */
    public static String getUserId() {
        return get(SmartConstant.USER_ID, String.class, null);
    }

    public static Long getUserIdLong() {
        String userId = getUserId();
        return Long.valueOf(userId);
    }

    /**
     * 用户ID
     *
     * @param userId 用户ID
     */
    public static void setUserId(String userId) {
        set(SmartConstant.USER_ID, userId);
    }

    /**
     * 登录账号
     *
     * @return 登录账号
     */
    public static String getUserName() {
        return get(SmartConstant.USER_NAME, String.class);
    }

    /**
     * 登录账号
     *
     * @param userName 登录账号
     */
    public static void setUserName(String userName) {
        set(SmartConstant.USER_NAME, userName);
    }

    /**
     * 姓名
     *
     * @return 姓名
     */
    public static String getName() {
        return get(SmartConstant.NAME, String.class);
    }

    /**
     * 姓名
     *
     * @param name 姓名
     */
    public static void setName(String name) {
        set(SmartConstant.NAME, name);
    }

    /**
     * token
     *
     * @return token
     */
    public static String getToken() {
        return get(SmartConstant.SESSION_ACCESSTOKEN, String.class);
    }

    /**
     * token
     *
     * @param token token
     */
    public static void setToken(String token) {
        set(SmartConstant.SESSION_ACCESSTOKEN, token);
    }

    /**
     * 获取租户编码
     *
     * @return 租户编码
     */
    public static String getTenantId() {
        return get(SmartConstant.TENANT_ID, String.class, StringPool.EMPTY);
    }

    public static void setTenantId(String val) {
        set(SmartConstant.TENANT_ID, val);
    }
    //endregion

    //region 基础方法
    public static Map<String, Object> getLocalMap() {
        Map<String, Object> map = ThreadLocalUtil.getThreadLocal();
        if (map == null) {
            map = new ConcurrentHashMap<>(10);
            ThreadLocalUtil.set(map);
        }
        return map;
    }

    public static void setLocalMap(Map<String, Object> localMap) {
        ThreadLocalUtil.set(localMap);
    }

    public static <T> T get(String key, Class<T> type) {
        Map<String, Object> map = getLocalMap();
        return Convert.convert(type, map.get(key));
    }

    public static <T> T get(String key, Class<T> type, Object def) {
        Map<String, Object> map = getLocalMap();
        return Convert.convert(type, map.getOrDefault(key, String.valueOf(def == null ? StringPool.EMPTY : def)));
    }

    public static String get(String key) {
        Map<String, Object> map = getLocalMap();
        return (String) map.getOrDefault(key, StringPool.EMPTY);
    }

    public static void set(String key, Object value) {
        Map<String, Object> map = getLocalMap();
        map.put(key, value == null ? StringPool.EMPTY : value);
    }

    public static void remove() {
        ThreadLocalUtil.remove();
    }
    //endregion
}
